<html>
<head>
	<script type="text/javascript">
		var aJaxURL			= "server-side/view/districts.action.php";			//server side folder url
		var list_aJaxURL	= "server-side/view/districts/list.action.php";		//server side folder url
		var seoyURL			= "server-side/seoy/seoy.action.php";			//server side folder url
		var tName			= "example";										//table name
		var fName			= "add-edit-form";									//form name
		
		$(document).ready(function (){
			LoadTable();
			
			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "delete_button");
			
			SetEvents("add_button", "delete_button", "check-all", tName, fName, aJaxURL);
		});

        //SeoYyy
		$(document.body).click(function (e) {
        	$("#post_code").autocomplete("close");        	
        });

        $(document).on("click", ".combobox", function (event) {
	    	var i = $(this).text();
			$("#" + i).autocomplete( "search", "" );
			
		});
        
		function LoadTable(table){
			switch(table){
				case 'post_code_list':					
					var id = $("#district_id").val();
					/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
	                GetDataTable("post_code_list", list_aJaxURL, "get_list", 3, "id=" + id, 0, "", 0, "");
	                	                				
					break;
				default:
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
	                GetDataTable(tName, aJaxURL, "get_list", 3, "", 0, "", 0, "");
			}
		}
		
		function LoadDialog(form){
			switch(form){
				case 'add-edit-post-code-form':
						SeoY("post_code", seoyURL, "post_codes", "", 0);
						var buttons = {
					        "save": {
					            text: "დამატება",
					            id: "add-post-code",
					            click: function () {
					            }
					        },
					        "cancel": {
					            text: "დახურვა",
					            id: "cancel-dialog",
					            click: function () {
					                $(this).dialog("close");
					            }
					        }
					    };
						GetDialog("add-edit-post-code-form", 480, "auto", buttons);									
					break;
				default:	
				var buttons = {
				        "save": {
				            text: "შენახვა",
				            id: "add-district",
				            click: function () {
				            }
				        },
				        "cancel": {
				            text: "დახურვა",
				            id: "cancel-dialog",
				            click: function () {
				                $(this).dialog("close");
				            }
				        }
				    };
				GetDialog(fName, 480, "auto", buttons);
				var district_id = $("#district_id").val();
				
	            if( empty(district_id) ){
	                var local_id = GetLocalID;
	                $("#district_id").val( local_id );                    
	            }
	            LoadTable("post_code_list");
				GetButtons("add_post_code_button", "delete_post_code_button");
				SetEvent("add_post_code_button", "delete_post_code_button", "check-all-postcode", "post_code_list", "add-edit-post-code-form");            
			}
		}

		$(document).on("click", "#add-post-code", function (event) {
			param = new Object();
	    	
            //Action
	    	param.act	= "add_post_code";	    	
		    param.id	= $("#district_id").val();
		    param.pc	= $("#post_code").val();
		    
		    
			if(param.pc == ''){
				alert("შეავსეთ საფოსტო ინდექსი!");
			}else{
			    $.ajax({
			        url: list_aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
								LoadTable("post_code_list");
								$("#add-edit-post-code-form").dialog("close");
							}
						}
				    }
			    });
			}								
		});

		$(document).on("click", "#add-district", function (event) {
			param = new Object();
	    	
            //Action
	    	param.act	= "add_district";	    	
		    param.id	= $("#district_id").val();
		    param.nm	= $("#district_name").val();		    
		    param.com	= $("#comment").val();
		    
			if(param.nm == ""){
				alert("შეავსეთ უბნის სახელი!");
			}else{
			    $.ajax({
			        url: aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
								LoadTable();
								$("#add-edit-form").dialog("close");
							}
						}
				    }
			    });
			}							
		});		

		

		function GetLocalID(){
			var local_id;
			$.ajax({
		        url: aJaxURL,
        		async: false, //r-value
			    data: "act=get_local_id",
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{
							local_id = data.local_id;
						}
					}
			    }
		    });
	      	return local_id;
		}
		function SetEvent(add, dis, check, tname, fname, tname){
		    // Add Event
		    $("#" + add).on("click", function () {
		        $.ajax({
		            url: list_aJaxURL,
		            type: "POST",
		            data: "act=get_add_page",
		            dataType: "json",
		            success: function (data) {
		                if (typeof (data.error) != "undefined") {
		                    if (data.error != "") {
		                        alert(data.error);
		                    } else {
		                        $("#" + fname).html(data.page);
		                        if ($.isFunction(window.LoadDialog)) {
		                            //execute it
		                            LoadDialog(fname);
		                        }
		                    }
		                }
		            }
		        });
		    });

		    /* Disable Event */
		    $("#" + dis).on("click", function () {
		        var data = $(".check:checked").map(function () { //Get Checked checkbox array
		            return this.value;
		        }).get();

		        for (var i = 0; i < data.length; i++) {
		            $.ajax({
		                url: list_aJaxURL,
		                type: "POST",
		                data: "act=disable&id=" + data[i],
		                dataType: "json",
		                success: function (data) {
		                    if (data.error != "") {
		                        alert(data.error);
		                    } else {
		                        $("#" + check).attr("checked", false);
		                        LoadTable("post_code_list");
		                    }
		                }
		            });
		        }
		        //Reload Table        
		        if ($.isFunction(window.LoadTable)) {
		            //execute it
		            LoadTable(tname);
		        }
		    });

		    /* Check All */
		    $("#" + check).on("click", function () {
		    	$("#" + tname + " INPUT[type='checkbox']").prop("checked", $("#" + check).is(":checked"));
		    });
			
		}		
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
            	<h2 align="center">უბნები</h2>
	        	<div id="button_area">
	        		<button id="add_button">დამატება</button><button id="delete_button">წაშლა</button>
	        	</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min">№:</th>
                            <th style="width: 100%">უბნის დასახელება</th>
                            <th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_code" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_name" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
        </div>
    </div>
    
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="უბნები">
    	<!-- aJax -->
	</div>
    <div id="add-edit-post-code-form" class="form-dialog" title="საფოსტო ინდექსები">
    	<!-- aJax -->
	</div>	
</body>
</html>